'use strict';
// 引入工具库
const utils = require('utility');

const Controller = require('egg').Controller;

class AdminController extends Controller {


  // 添加管理员
  async addAdmin() {
    const { ctx } = this;
    const params = ctx.request.body;
    try {
      const ad = await ctx.model.Admin.findAll({
        where: {
          username: params.username,
          mobile: params.mobile
        }, raw: true
      });

      console.log('ad', ad);

      if (ad[0]) return ctx.body = { success: false, info: '管理员已存在' };
      try {
        await ctx.model.Admin.create({
          rid: params.rid,
          username: params.username,
          password: utils.md5(params.password),
          mobile: params.mobile,
          email: params.email,
          add_time: Date.now()
        });

        ctx.body = { success: true, info: '添加管理员成功' };
      } catch (e) {
        ctx.body = { success: false, info: '添加管理员失败' };
        console.log('ee', e);
      }

    } catch (e) {
      ctx.body = { success: false, info: '管理员不存在' };
      console.log('e', e);
    }


  }

  // 管理员登录
  async login() {
    const { ctx, app } = this;
    const { username, password } = ctx.request.body;
    if (!username) return ctx.body = { success: false, info: '请输入用户名' };
    if (!password) return ctx.body = { success: false, info: '请输入密码' };

    try {
      const res = await ctx.model.Admin.findOne({
        where: {
          username: username
        }, raw: true

      });
      // console.log('ress', res);


      let token = app.jwt.sign({ ...ctx.request.body }, app.config.jwt.secret);
      // 把session加入到缓存中
      ctx.session.userinfo = { ...ctx.request.body };
      if (res) {
        if (utils.md5(password) === res.password) {
          ctx.body = {
            success: true,
            info: '登录成功',
            data: {
              id: res.id,
              rid: res.rid,
              username: res.username,
              password: res.password,
              mobile: res.mobile,
              email: res.email,
              add_time: res.add_time,
              update_time: res.update_time,
              del_time: res.del_time,
              token: `Bearer ${token}`
            }
          };
        } else {
          ctx.body = { success: false, info: '密码错误' };
        }
      } else {
        ctx.body = { success: false, info: '用户不存在' };
      }

    } catch (e) {
      ctx.body = { success: false, info: '登录失败' };
    }
  }

  // token的验证
  async verifyToken() {
    const { ctx, app } = this;

    const { username } = ctx.request.body;

    const userinfo = await ctx.model.Admin.findOne({
      where: {
        username: username
      }
    });
    console.log('uu', userinfo);
    ctx.body = { success: true, data: userinfo };

  }

  // 管理员登出
  async logout() {
    const { ctx } = this;
    ctx.session = null;
    console.log('session', ctx.session);
    ctx.body = {success:true,info:'注销成功'};
  }


  // 管理员修改密码
  async editPwd() {
    const { ctx } = this;
    let { adminId, pwd } = ctx.request.body;
    const res = await ctx.model.Admin.findOne({
      where: {
        id: adminId
      }, raw: true
    });
    console.log('resss', res);
    console.log('原来的密码', res.password);
    let updatePwd = {};
    pwd = utils.md5(pwd);
    if (pwd) updatePwd.password = pwd;
    console.log('改的密码', pwd);
    if (res.password === pwd) return ctx.body = { success: false, info: '修改的密码不能相同' };
    try {
      await ctx.model.Admin.update(updatePwd, {
        where: {
          id: adminId
        }
      });
      const newPwd = await ctx.model.Admin.findOne({
        where: {
          id: adminId
        }, raw: true
      });
      console.log('修改成功的密码', newPwd);

      ctx.body = { success: true, info: '修改密码成功', data: newPwd };
    } catch (e) {
      ctx.body = { success: false, info: '修改密码失败', msg: e.message };
    }


  }


}

module.exports = AdminController;
